﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DebitNote.aspx.cs" Inherits="Main_ServiceInvoice_DebitNote" %>

<%@ Register Src="~/User Controls/PageHeading.ascx" TagName="heading" TagPrefix="acc" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="AjaxControl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <link href="../../../Styles/accounts.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript" language="javascript">
        function isNumberKey(control_id, evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode
            if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 45 || charCode > 47))
                return false;

            if (charCode == 46) {
                var dotcount = document.getElementById(control_id).value.split('.').length - 1;
                if (dotcount >= 1) {
                    return false;
                }
            }
            return true;
        }
        function EnabledTabKey(id, e) {
            var keycode;
            if (window.event) {
                keycode = window.event.keyCode;
            }
            else if (e) {
                keycode = e.which;
            }
            else {
                return true;
            }

            if (keycode == 9) {
                // if is the tab key                
                return true;
            }
            else {
                return false;
            }
        }
        function GetLastNumber(sText) {
            var Char;
            var lastRow = 0;
            for (i = 0; i < sText.length; i++) {
                Char = sText.charAt(i);
                if (!isNaN(parseFloat(Char))) {
                    lastRow = sText.toString().substr(i);
                    break;
                }
            }
            return lastRow;
        }

        function checkItemRepetion(id) {
            var flag = true;
            var ddl = document.getElementById(id);
            var e = document.getElementById("ddlCustomer");
            var strCustomer = e.options[e.selectedIndex].value;
            var currentValue = ddl.options[ddl.selectedIndex].value;
            var Count = document.getElementById("hiddentControl").value;
            for (var i = 1; i <= Count; i++) {
                var previous = document.getElementById("ddlService" + i);
                var previousValue = previous.options[previous.selectedIndex].value
                if (ddl != previous) {
                    if (currentValue == previousValue && currentValue != '0') {
                        alert('Please Select Unique Service');
                        ddl.selectedIndex = 0;
                        flag = false;
                        break;
                    }
                }
            }

            if (flag == true) {
                setTimeout('__doPostBack(\'' + id + '\',\'\')', 0);
            }
            return flag;
        }

        function calTaxableNonTaxable() {
            var Count = document.getElementById("hiddentControl").value;
            var rdIndex = 0;
            var rdlist = document.getElementById("rbtDebitNoteType");
            var inputs = rdlist.getElementsByTagName("input");
            for (var x = 0; x < inputs.length; x++) {
                if (inputs[x].checked) {
                    rdIndex = inputs[x].value;
                    break;
                }
            }
            var ServiceTaxRate = 0;
            var PECessRate = 0;
            var HECessRate = 0;
            ServiceTaxRate = document.getElementById("hidServiceTaxRate").value;
            PECessRate = document.getElementById("hidPECessRate").value;
            HECessRate = document.getElementById("hidHECessRate").value;
            var totalServiceTax = 0;
            var totalPECessTax = 0;
            var totalHECessTax = 0;
            var totalTaxable = 0;
            var totalNonTaxable = 0;
            if (rdIndex == "0") {
                for (var i = 1; i <= Count; i++) {
                    var IsTaxable = document.getElementById("hidChkTaxable" + i).value;                    
                    var Taxable = 0;
                    var PECessValue = 0;
                    var HECessValue = 0;
                    var NonTaxable = 0;
                    if (document.getElementById("ddlService" + i).value != '0') {
                        if (document.getElementById("txtAmountService" + i).value != '') {
                            if (IsTaxable == '1') {
                                Taxable = parseFloat(document.getElementById("txtAmountService" + i).value);
                                totalTaxable += parseFloat(Taxable);
                                totalServiceTax += (parseFloat(Taxable) * parseFloat(ServiceTaxRate) / 100);
                                PECessValue = (parseFloat(Taxable) * parseFloat(PECessRate) / 100).toFixed(2);
                                HECessValue = (parseFloat(Taxable) * parseFloat(HECessRate) / 100).toFixed(2);
                                totalHECessTax += parseFloat(HECessValue);
                                totalPECessTax += parseFloat(PECessValue);
                            }
                            else {
                                NonTaxable = parseFloat(document.getElementById("txtAmountService" + i).value);
                                totalNonTaxable += parseFloat(NonTaxable);
                            }
                        }
                    }
                }
            }
            else if (rdIndex == "1") {
                for (var i = 1; i <= Count; i++) {
                    var NonTaxable = 0;
                    if (document.getElementById("ddlService" + i).value != '0') {
                        if (document.getElementById("txtAmountService" + i).value != '') {
                            NonTaxable = parseFloat(document.getElementById("txtAmountService" + i).value);
                            totalNonTaxable += parseFloat(NonTaxable);
                        }
                    }
                }
            }
            else if (rdIndex == "2") {
                for (var i = 1; i <= Count; i++) {
                    var ServiceTax = 0;
                    var PECessValue = 0;
                    var HECessValue = 0;
                    if (document.getElementById("ddlService" + i).value != '0') {
                        if (document.getElementById("txtAmountService" + i).value != '') {
                            ServiceTax = document.getElementById("txtAmountService" + i).value;
                            totalServiceTax += parseFloat(ServiceTax);
                            PECessValue = (parseFloat(ServiceTax) * parseFloat(PECessRate) / 100).toFixed(2);
                            HECessValue = (parseFloat(ServiceTax) * parseFloat(HECessRate) / 100).toFixed(2);
                            document.getElementById("txtPECess" + i).value = PECessValue;
                            document.getElementById("txtHECess" + i).value = HECessValue;
                            totalHECessTax += parseFloat(HECessValue);
                            totalPECessTax += parseFloat(PECessValue);
                        }
                    }
                }
            }
            document.getElementById("txtTaxable").value = totalTaxable.toFixed(2);
            document.getElementById("txtNonTaxable").value = totalNonTaxable.toFixed(2);            
            document.getElementById("txtServiceTax").value = totalServiceTax.toFixed(2);
            document.getElementById("txtPECess").value = totalPECessTax.toFixed(2);
            document.getElementById("txtHECess").value = totalHECessTax.toFixed(2);
            document.getElementById("txtTotalAmount").value = (totalTaxable + totalNonTaxable + totalServiceTax + totalPECessTax + totalHECessTax).toFixed(2);
           
            return true;
        }
        function SpecifiedService(id) {
            var flag = true;
            var rowno = GetLastNumber(id.toString());
            if (document.getElementById("ddlService" + rowno).value == '0') {
                alert('Specify Service');
                document.getElementById("ddlService" + rowno).focus();
                document.getElementById("txtAmountService" + rowno).value = '0.00';
                flag = false;
            }
            return flag;
        }
        function ValidateInputs() {
            var flag = true;
            if (document.getElementById("ddlBillFormat").value == '0') {
                alert('Specify Format');
                document.getElementById("ddlBillFormat").focus();
                flag = false;
            }
            else if (document.getElementById("ddlCustomer").value == '0') {
                alert('Specify Customer');
                document.getElementById("ddlCustomer").focus();
                flag = false;
            } 
            else if (document.getElementById("txtDebitNoteNo").value == '') {
                alert('Specify Debit Note Number');
                document.getElementById("txtDebitNoteNo").focus();
                flag = false;
            }
            else if (document.getElementById("txtDate").value == '' || document.getElementById("txtDate").value == '__/__/____') {
                alert('Specify Date');
                document.getElementById("txtDate").focus();
                flag = false;
            }                     
            else if (document.getElementById("ddlService1").value == '0') {
                alert('Specify Service');
                document.getElementById("ddlService1").focus();                
                flag = false;
            }
            else if (document.getElementById("txtAmountService1").value == '0.00' || document.getElementById("txtAmountService1").value == '') {
                alert('Specify Amount');
                document.getElementById("txtAmountService1").focus();                
                flag = false;
            }            
            return flag;
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="scriptmanger1" runat="server">
    </asp:ScriptManager>
    <div class="divmain" style="width: 100%;">
        <acc:heading ID="PageHead" runat="server" HeadingText="Debit Note" />
    </div>
    <div style="margin-left: 5%; margin-right: 5%; width: 90%" align="center">
        <span style="color: #f29e08; font-size: 10; font-weight: bold;">
            <asp:Label ID="lblMsg" runat="server" Font-Bold="true"></asp:Label></span>
        <asp:HiddenField ID="hiddentControl" runat="server" Value="1" />
    </div>
    <div style="width: 100%">
        <table style="width: 100%">
            <tr>
                <td style="width: 50%">
                    <table cellpadding="2" class="tablecontent" cellspacing="2">
                        <tr>
                            <td style="width: 35%">
                                Bill Format<span style="color: Red;">*</span>
                            </td>
                            <td colspan="3" style="width: 65%">
                                <asp:DropDownList ID="ddlBillFormat" runat="server" CssClass="dropdown" Width="100%"
                                    OnSelectedIndexChanged="ddlBillFormat_SelectedIndexChanged" AutoPostBack="true">
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td style="width: 35%">
                                Debit Note No.<span style="color: red">*</span>
                            </td>
                            <td style="width: 20%">
                                <asp:TextBox ID="txtPrefix" runat="server" Width="100px" CssClass="input" ToolTip="Prefix"
                                    onkeydown="return EnabledTabKey(this.id, event)" BackColor="#eaf2f2"></asp:TextBox>
                            </td>
                            <td style="width: 25%" align="center">
                                <asp:TextBox ID="txtDebitNoteNo" runat="server" Width="100px" CssClass="input"></asp:TextBox>
                            </td>
                            <td style="width: 20%">
                                <asp:TextBox ID="txtSuffix" runat="server" Width="100px" CssClass="input" ToolTip="Suffix"
                                    onkeydown="return EnabledTabKey(this.id, event)" BackColor="#eaf2f2"></asp:TextBox>
                            </td>
                        </tr>
                    </table>
                </td>
                <td style="width: 50%">
                    <table cellpadding="2" class="tablecontent">
                        <tr>
                            <td style="width: 20%">
                              Customer<span style="color: Red;">*</span>
                            </td>
                            <td colspan="2">
                                <asp:DropDownList ID="ddlCustomer" runat="server" CssClass="dropdown" Width="100%">
                                </asp:DropDownList>
                            </td>
                        </tr>
                        <tr>
                            <td style="width: 20%">
                                Ref.No.
                            </td>
                            <td style="width: 30%">
                                <asp:TextBox ID="txtRefNo" runat="server" CssClass="input"></asp:TextBox>
                            </td>
                            <td style="width: 50%">
                                Date<span style="color: Red;">*</span>
                                <asp:TextBox ID="txtDate" runat="server" CssClass="input" Width="60%" OnTextChanged="txtDate_TextChanged" onkeydown="return EnabledTabKey(this.id, event)" AutoPostBack="true" MaxLength="10"></asp:TextBox>
                                <ajaxcontrol:calendarextender id="calender" runat="server" popupbuttonid="txtDate"
                                    targetcontrolid="txtDate" format="dd/MM/yyyy"></ajaxcontrol:calendarextender>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
        <table cellpadding="2" style="width: 100%">
            <tr>
                <td>                   
                    <table cellpadding="2" width="100%" style="border: 1px">
                        <tr>
                            <td style="width: 100%">
                                <asp:RadioButtonList ID="rbtDebitNoteType" runat="server" RepeatDirection="Horizontal" AutoPostBack="true"
                                    OnSelectedIndexChanged="rbtDebitNoteType_SelectedIndexChanged">
                                    <asp:ListItem Selected="True" Value="0" Text="Difference In Bill"></asp:ListItem>
                                    <asp:ListItem Value="1" Text="Expense Reimbursement" style="padding-left:20px"></asp:ListItem>
                                    <asp:ListItem Value="2" Text="Service Tax Only" style="padding-left:20px"></asp:ListItem>
                                </asp:RadioButtonList>
                            </td>
                        </tr>
                    </table>                    
                </td>
            </tr>
            <tr>
                <td>
                    <table id="tblService" runat="server" width="100%" class="tablecontent" cellpadding="2">
                        <tr>
                            <td style="border-right: solid 1px #818181; width: 30%" class="headtd">
                                Service
                            </td>
                            <td style="border-right: solid 1px #818181; width: 30%" class="headtd">
                                Additional Description
                            </td>
                            <td style="border-right: solid 1px #818181; width: 15%" class="headtd">
                                <asp:Label ID="lblAmount" runat="server" Text="Amount"></asp:Label>
                            </td>
                            <td class="headtd" style="width: 13%">
                                <asp:Label ID="lblPECess" runat="server" Text="P.E.Cess"></asp:Label>
                            </td>
                            <td class="headtd" style="width: 12%">
                                <asp:Label ID="lblHECess" runat="server" Text="H.E.Cess"></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td style="width: 30%">
                                <asp:DropDownList ID="ddlService1" runat="server" CssClass="dropdown" Width="98%"
                                    OnSelectedIndexChanged="ddlService_SelectedIndexChanged" AutoPostBack="true" onchange="checkItemRepetion(this.id)">
                                </asp:DropDownList>
                                <asp:HiddenField ID="hidChkTaxable1" runat="server" />
                            </td>
                            <td style="width: 30%">
                                <asp:TextBox ID="txtAdditional1" runat="server" CssClass="input" Width="98%"></asp:TextBox>
                            </td>
                            <td style="width: 15%">
                                <asp:TextBox ID="txtAmountService1" runat="server" CssClass="inputnumber" Width="98%"
                                    onKeyPress="return isNumberKey(this.id,event)" AutoCompleteType="None" onblur="calTaxableNonTaxable();" onfocus="return SpecifiedService(this.id);" MaxLength="15" ></asp:TextBox>
                            </td>
                            <td style="width: 13%">
                                <asp:TextBox ID="txtPECess1" runat="server" CssClass="inputnumber" Width="98%" onkeydown="return EnabledTabKey(this.id, event)"></asp:TextBox>
                            </td>
                            <td style="width: 12%">
                                <asp:TextBox ID="txtHECess1" runat="server" CssClass="inputnumber" Width="98%" onkeydown="return EnabledTabKey(this.id, event)"></asp:TextBox>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td align="right" style="width: 100%">
                    <asp:Button ID="btnAddMore" runat="server" Text="Add More" CssClass="button" OnClick="btnAddMore_Click" />&nbsp;&nbsp;
                    <asp:Button ID="btnDelete" runat="server" Text="Delete" CssClass="button" OnClick="btnDelete_Click" Visible="false" />&nbsp;&nbsp;
                </td>
            </tr>
            <tr>
                <td style="width: 100%">
                    <table cellpadding="2" class="tablecontent" width="100%">
                        <tr>
                            <td style="width: 50%;" valign="top">
                                <table cellpadding="2" width="100%">
                                    <tr>
                                        <td style="width: 20%">
                                            Narration
                                        </td>
                                        <td style="width: 80%">
                                            <asp:TextBox ID="txtNarration" runat="server" CssClass="input" Width="95%"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Remarks
                                        </td>
                                        <td>
                                            <asp:TextBox ID="txtRemarks" runat="server" CssClass="input" Width="95%"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Prepared By
                                        </td>
                                        <td>
                                            <asp:TextBox ID="txtPreparedBy" runat="server" CssClass="input" Width="95%"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Checked By
                                        </td>
                                        <td>
                                            <asp:TextBox ID="txtCheckedBy" runat="server" CssClass="input" Width="95%"></asp:TextBox>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                            <td colspan="3" style="width: 50%">
                                <table cellpadding="2" style="width: 100%">
                                    <tr>
                                        <td colspan="2">
                                            Taxable Amount
                                        </td>
                                        <td align="right">
                                            <asp:TextBox ID="txtTaxable" runat="server" CssClass="DisableInputNumber"  Width="95%" onkeydown="return EnabledTabKey(this.id, event)"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="2">
                                            Non Taxable Amount
                                        </td>
                                        <td align="right">
                                            <asp:TextBox ID="txtNonTaxable" runat="server" CssClass="DisableInputNumber"  Width="95%" onkeydown="return EnabledTabKey(this.id, event)"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td style="width: 30%">
                                            <asp:Label ID="lblServiceTax" runat="server" Text="Service Tax"></asp:Label>
                                            <asp:HiddenField ID="hidServiceTaxLid" runat="server" Value="0" />
                                            <asp:HiddenField ID="hidServiceTaxRate" runat="server" Value="0" />
                                        </td>
                                        <td style="widows: 20%" align="right">
                                            <asp:TextBox ID="txtServiceTaxPer" runat="server" Width="80px" CssClass="DisableInputNumber"  MaxLength="2" onKeyUp="calTaxableNonTaxable()"></asp:TextBox>
                                        </td>
                                        <td style="width: 50%" align="right">
                                            <asp:TextBox ID="txtServiceTax" runat="server" CssClass="DisableInputNumber"  Width="95%" onkeydown="return EnabledTabKey(this.id, event)"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="lblPECess1" runat="server" Text="P.E.Cess"></asp:Label>
                                            <asp:HiddenField ID="hidPECessLid" runat="server" Value="0" />
                                            <asp:HiddenField ID="hidPECessRate" runat="server" Value="0" />
                                        </td>
                                        <td align="right">
                                            <asp:TextBox ID="txtPECessPer" runat="server" Width="80px" CssClass="DisableInputNumber"   BackColor="#eaf2f2" MaxLength="2" onKeyPress="return isNumberKey(this.id,event)" onKeyUp="calTaxableNonTaxable()"></asp:TextBox>
                                        </td>
                                        <td align="right">
                                            <asp:TextBox ID="txtPECess" runat="server" CssClass="DisableInputNumber"  Width="95%" onkeydown="return EnabledTabKey(this.id, event)"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="lblHECess1" runat="server" Text="H.E.Cess"></asp:Label>
                                            <asp:HiddenField ID="hidHECessLid" runat="server" Value="0" />
                                            <asp:HiddenField ID="hidHECessRate" runat="server" Value="0" />
                                        </td>
                                        <td align="right">
                                            <asp:TextBox ID="txtHECessPer" runat="server" Width="80px" CssClass="DisableInputNumber"  MaxLength="2" onKeyPress="return isNumberKey(this.id,event)" onKeyUp="calTaxableNonTaxable()"></asp:TextBox>
                                        </td>
                                        <td align="right">
                                            <asp:TextBox ID="txtHECess" runat="server" CssClass="DisableInputNumber"  Width="95%" onkeydown="return EnabledTabKey(this.id, event)"></asp:TextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <asp:Label ID="lblTotalAmount" runat="server" Text="Total Amount"></asp:Label>
                                        </td>
                                        <td align="right">
                                            &nbsp;
                                            <%--<asp:Button ID="btnSetOff" runat="server" Text="Set Off" CssClass="button" />--%>
                                        </td>
                                        <td align="right">
                                            <asp:TextBox ID="txtTotalAmount" runat="server" CssClass="inputnumber" Width="95%"
                                                onkeydown="return EnabledTabKey(this.id, event)" BackColor="#eaf2f2"></asp:TextBox>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
        <table cellpadding="2" width="100%">
            <tr>
                <td align="right">
                    <asp:Button ID="btnSave" runat="server" Text="Save" CssClass="button" OnClick="btnSave_Click" OnClientClick="return ValidateInputs();" />&nbsp;&nbsp;
                    <asp:Button ID="btnClose" runat="server" Text="Close" OnClick="btnClose_Click" CssClass="button" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
